import store from "@/store";
import { Session } from "@/utils/storage"; // 验权

const whiteList = ["/login", "/404"]; // 不重定向白名单
const addRouters = (router, routers) => {
  routers.forEach((r) => {
    router.addRoute(r);
  });
};

export const Permission = (router) => {
  router.beforeEach((to, from, next) => {
    const tokenInfo = Session.get("tokenInfo");
    if (tokenInfo.access_token) {
      // 判断是否有token
      if (to.path === "/login") {
        next({ path: "/" });
      } else {
        if (store.getters["user/roles"].length === 0) {
          // 判断当前用户是否已拉取完user_info信
          store
            .dispatch("user/GetInfo")
            .then((res) => {
              const roles = store.getters["user/roles"];
              store.dispatch("app/getAllFilters");
              store.dispatch("permission/GenerateRoutes", roles).then(() => {
                // 生成可访问的路由表
                // console.log(
                //   "store.getters.addRouters",
                //   store.getters["permission/addRouters"]
                // );
                addRouters(router, store.getters["permission/addRouters"]);
                next({ ...to });
                // first = true;
              });
            })
            .catch(() => {
              console.log(store, 'store')
              store.dispatch("user/FedLogOut").then(() => {
                // ElMessage.error("验证失败,请重新登录");
                next({ path: "/login" });
              });
            });
            
        } else {
          next();
        }
      }
    } else {
      if (whiteList.indexOf(to.path) !== -1) {
        // 在免登录白名单，直接进入
        next();
      } else {
        next("/login"); // 否则全部重定向到登录页
      }
    }
  });

  router.afterEach(() => {});
};
